package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import top.zywork.common.BeanUtils;
import top.zywork.dto.PagerDTO;
import top.zywork.query.GoodsCommentPicQuery;
import top.zywork.query.UserGoodsCommentQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.GoodsCommentPicService;
import top.zywork.service.UserGoodsCommentService;
import top.zywork.vo.GoodsCommentPicVO;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.UserGoodsCommentVO;

import java.util.ArrayList;
import java.util.List;

/**
 * UserGoodsCommentController控制器类<br/>
 *
 * 创建于2019-07-27<br/>
 *
 * @author http://zywork.top 危锦辉
 * @version 1.0
 */
@RestController
@RequestMapping("/user-goods-comment")
@Slf4j
public class UserGoodsCommentController extends BaseController {

    private UserGoodsCommentService userGoodsCommentService;

    private GoodsCommentPicService goodsCommentPicService;

    @GetMapping("admin/multi/{id}")
    public ResponseStatusVO listById(@PathVariable("id") Long id) {
        PagerDTO pagerDTO = userGoodsCommentService.listById(id);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), UserGoodsCommentVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = userGoodsCommentService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), UserGoodsCommentVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody UserGoodsCommentQuery userGoodsCommentQuery) {
        PagerDTO pagerDTO = userGoodsCommentService.listAllByCondition(userGoodsCommentQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), UserGoodsCommentVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody UserGoodsCommentQuery userGoodsCommentQuery) {
        // 根据查询条件获取对应的用户评论信息
        PagerDTO pagerDTO = userGoodsCommentService.listPageByCondition(userGoodsCommentQuery);
        List<UserGoodsCommentVO> returnList = new ArrayList<>();
        List<UserGoodsCommentVO> userGoodsCommentVOList = BeanUtils.copy(pagerDTO.getRows(), UserGoodsCommentVO.class);
        for (UserGoodsCommentVO userGoodsCommentVO : userGoodsCommentVOList) {
            GoodsCommentPicQuery goodsCommentPicQuery = new GoodsCommentPicQuery();
            goodsCommentPicQuery.setCommentId(userGoodsCommentVO.getGoodsCommentId());
            // 根据单个评论信息查询出对应的图片信息
            PagerDTO pagerDTO1 = goodsCommentPicService.listAllByCondition(goodsCommentPicQuery);
            List<Object> objectList = pagerDTO1.getRows();
            List<GoodsCommentPicVO> goodsCommentPicVOList = BeanUtils.copy(objectList, GoodsCommentPicVO.class);
            StringBuilder imgSB = new StringBuilder();
            for (GoodsCommentPicVO goodsCommentPicVO : goodsCommentPicVOList) {
                if (StringUtils.isEmpty(imgSB.toString())) {
                    imgSB.append(goodsCommentPicVO.getPicUrl());
                } else {
                    imgSB.append(",").append(goodsCommentPicVO.getPicUrl());
                }
            }
            userGoodsCommentVO.setImageList(imgSB.toString().split(","));
            // 将图片信息数组重新放入新的list
            returnList.add(userGoodsCommentVO);
        }
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyListObj(returnList, UserGoodsCommentVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 查询商品的评论信息
     * @param userGoodsCommentQuery
     * @return
     */
    @PostMapping("any/pager-cond")
    public ResponseStatusVO anyListPageByCondition(@RequestBody UserGoodsCommentQuery userGoodsCommentQuery) {
        return listPageByCondition(userGoodsCommentQuery);
    }

    /**
     * 用户查询自己的商品评论信息
     * @param userGoodsCommentQuery
     * @return
     */
    @PostMapping("user/pager-cond")
    public ResponseStatusVO userListPageByCondition(@RequestBody UserGoodsCommentQuery userGoodsCommentQuery) {
        userGoodsCommentQuery.setUserId(SecurityUtils.getJwtUser().getUserId());
        return listPageByCondition(userGoodsCommentQuery);
    }

    @Autowired
    public void setUserGoodsCommentService(UserGoodsCommentService userGoodsCommentService) {
        this.userGoodsCommentService = userGoodsCommentService;
    }

    @Autowired
    public void setGoodsCommentPicService(GoodsCommentPicService goodsCommentPicService) {
        this.goodsCommentPicService = goodsCommentPicService;
    }
}
